package org.pwwong.fxscalautils.components

import org.pwwong.fxscalautils.Predef._
import org.pwwong.fxscalautils.DragManager

import org.pwwong.scalautils.signals.Predef._
import org.pwwong.scalautils.event._

trait Draggable[DragDataType] extends Component {
  import Component._
  
  protected var _dragData:Option[DragDataType] = None
  def dragData[T<:DragDataType](data:T) = _dragData = Some(data)
  def dragData = _dragData.get
  
  private var _useDragLayer:Boolean = false
  def useDragLayer( udl:Boolean ) = _useDragLayer = !udl
  def useDragLayer:Boolean = _useDragLayer
  
  val onDragStart = Event[DragDataType]
  val onDragComplete = Event[DragDataType]
  
  private var _isDragging:Boolean = false
  
  onMouseDragged(this){ evTarg => if(!_isDragging && _dragData != None) {
                                    _isDragging = true
                                    if(_useDragLayer) DragManager.DragLayer.addDragComponent(this)
                                  }
                                  x( Left(evTarg._1.getX px) )
                                  y( Top(evTarg._1.getY px) ) 
  }
}
